package com.yasaka.stock.auth;

import com.yasaka.stock.vo.TokenVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


/**
 * OAuthTokenUtils
 * Token管理
 */
public class AuthTokenManager {
    private static final String APP_ID = "kFoqFQHbEt26Ej1URMC6si29Og0bzz1MBwpNHKuwIp8";
    private static final String APP_SECRET = "dU5y1ACTjvy7swf08LLiHQ04NAjf3GhyH1dZ8zLHYvg";
    private static final String SERVER_SECRET = "QtiBdTBDdlbU8xlylkSf0iLbCHbBk4QzyQG0kVNtU";


    public static TokenVo generateToken(String appId, String appSecret){
        String id = UUID.randomUUID().toString().replace("-", "");
//        Date now = new Date();
        Date exp = new Date(System.currentTimeMillis() + (1000*60*60*2)); // 30 seconds
        Map<String,Object> claims = new HashMap<String,Object>();
        claims.put("appId",appId);
        claims.put("appSecret",appSecret);
        String token = Jwts.builder()
//                .setClaims(claims)
//                .setIssuedAt(now)           //iat: jwt的签发时间
//                .setSubject(subject)        //sub(Subject)：代表这个JWT的主体，即它的所有人，这个是一个json格式的字符串，可以存放什么userid，roldid之类的，作为什么用户的唯一标志。
//                .setIssuedAt(now)
//                .setNotBefore(now)
                .setExpiration(exp)
                .setId(id)                  //设置jti(JWT ID)：是JWT的唯一标识，根据业务需要，这个可以设置为一个不重复的值，主要用来作为一次性token,从而回避重放攻击。
                .signWith(SignatureAlgorithm.HS256, SERVER_SECRET)
                .compact();
        TokenVo vo = new TokenVo();
        vo.setAccess_token(token);
        vo.setExpires_in(7200);
        return vo;
    }

//    public static void verifyToken(String token){
//        Claims claims = Jwts.parser().
//                setSigningKey(DatatypeConverter.parseBase64Binary(SERVER_SECRET))
//                .parseClaimsJws(token).getBody();
//        System.out.println("----------------------------");
//        System.out.println("ID: " + claims.getId());
//        System.out.println("Subject: " + claims.getSubject());
//        System.out.println("Issuer: " + claims.getIssuer());
//        System.out.println("IssuedAt: " + claims.getIssuedAt());
//        System.out.println("Expiration: " + claims.getExpiration());
//    }

    public static void main(String[] args) {
        System.out.println(generateToken(AuthTokenManager.APP_ID, AuthTokenManager.APP_SECRET));
//        String token = generateToken(AuthTokenManager.APP_ID, AuthTokenManager.APP_SECRET);
//        verifyToken(token);
//        String str = "eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1MjMyNjM1NTUsImp0aSI6IlRFU1QxMjMifQ.kFoqFQHbEt26Ej1URMC6si29Og0bzz1MBwpNHKuwIp8";
//                    eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1MjMyNjM5MjQsImp0aSI6IjFlZWUyMWEwYzk0NjRlZTM5YTdmOGI4YmFjNzMyYjdiIn0.dU5y1ACTjvy7swf08LLiHQ04NAjf3GhyH1dZ8zLHYvg

//        String str = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI1MDBiMzA1MmMyYmE0ZWM4YWM1YjFiNGQ0MjhhOTc5NyIsInN1YiI6InN1YmplY3QiLCJpc3MiOiJEYW5pZWwiLCJpYXQiOjE1MjMyNjIzMTMsIm5iZiI6MTUyMzI2MjMxMywiZXhwIjoxNTIzMjYyMzQzfQ.QtiBdTBDdlbU8xl-y-lkSf0iLbCHbBk4QzyQG0kVNtU";
//        verifyToken(str);
    }

}